package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Properties;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private Properties pageHelperProperties;

    @Override
    @Transactional//事务
    public void saveWithDish(SetmealDTO setmealDTO) {

        //1.将DTO套餐数据转移到Entity
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //2.向套餐表插入数据
        setmealMapper.insert(setmeal);

        //3.获取插入套餐的id
        Long setmealId = setmeal.getId();

        //4.获取套餐的菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        //5.遍历套餐菜品数据，为每个菜品设置套餐id
        //判断套餐菜品是否为空
        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            //6. 批量插入套餐菜品关系数据到 setmeal_dish 表
            setmealDishMapper.insertBatch(setmealDishes);

        }

    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        //调用Mapper分页查询
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());

    }

    /**
     * 套餐起售停售
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //如果是要起售套餐(status=1)，则判断套餐内是否有停售菜品
        if(status.equals(StatusConstant.ENABLE)){
            //查询套餐关联的所有菜品
            List<Dish> dishList = setmealMapper.getBySetmealId(id);

            //判断查询到的菜品列表是否为空
            if(dishList != null && dishList.size() > 0){
                for (Dish dish : dishList) {
                    if(dish.getStatus().equals(StatusConstant.DISABLE)){
                        //抛出业务异常，提示“套餐包含未起售的菜品，套餐无法起售”
                        throw new SetmealEnableFailedException("套餐包含未起售的菜品，套餐无法起售");
                    }
                }
            }
        }

        //如果是停售套餐(status=0)，或者起售通过检查，则更新套餐状态
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.updateSetmeal(setmeal);

    }

    @Override
    public void deleteBatch(List<Long> ids) {
        //1.判断当前套餐是否在起售，起售则无法删除
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if(setmeal.getStatus().equals(StatusConstant.ENABLE)){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        ids.forEach(setmealId -> {
            setmealMapper.deleteById(setmealId);
            setmealDishMapper.deleteBySetmealId(setmealId);
        });


    }

    @Override
    public SetmealVO getByIdWithDish(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);

        //将setmeal和setmealDishes封装到SetmealVO中并返回
        SetmealVO setmealVO = new SetmealVO();
        //进行属性拷贝
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        //使用setmeal实体封装数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //1.修改套餐表并且获取修改的套餐id
        setmealMapper.updateSetmeal(setmeal);
        Long setmealId = setmeal.getId();

        //2.删除套餐和菜品的关联关系
        setmealDishMapper.deleteBySetmealId(setmealId);

        //获取新的套餐菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));

        //3.插入新的套餐和菜品的关联关系
        setmealDishMapper.insertBatch(setmealDishes);
    }
}
